// 引入定义 store 的方法
import { defineStore } from 'pinia'

import router from '@/router/index'
import { getShareNoteList, writeNote, getArticleById,reqUploadFile} from '@/api/index'
import { ref } from 'vue'

/**
 * pinia 中使用 defineStore 方法来定义 store
 * 第一个参数是应用程序中 store 唯一的 id 也可以理解为模块名
 * 第二个参数 是一个对象 store 的配置项 
 */
export const myNoteStore = defineStore('notes', () =>{
    let noteList = ref({})
    let noteCateList = ref({})
    let fileUrl = ref(null)
    // 获取笔记列表
    const shareNoteList = async(id) => {
        let result = await getShareNoteList(id)
        if(result.status == 0) {
            noteList.value = result.data
        }
    }
    // 添加笔记
    const writeNoteText = async(params = {}) => {
        let result = await writeNote(params)
        if(result.status == 0) {
            console.log('成功了！');
        }
    }
    // 获取笔记分类
    const articleById = async(note_id) => {
        let result = await getArticleById(params)
        if (result.status == 0) {
            noteCateList.value = result.data
        } else {
            console.log(result.message);
        }
    }

    // 上传文件
    const uploadFile = async(params = {})=>{
        let result = await reqUploadFile(params)
        if (result.code == 200) {

            fileUrl.value = result.location
            console.log(fileUrl.value);
        }else{
            console.log(result.message);
        }
    }

    return { shareNoteList, writeNoteText, articleById, noteList, noteCateList,uploadFile,fileUrl }
})